Skip to content

Pass storage to constructors instead of returning value - #686

Merged
nunoplopes merged 20 commits into
Cpp2Rust:masterfrom
lucic71:this-ptr
Nov 30, 2026
Merged

nunoplopes merged 20 commits into
Cpp2Rust:masterfrom
lucic71:this-ptr

Conversation

@lucic71

@lucic71 lucic71 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Part of #674, closes #248

Returning values from the constructor does not follow the C++ semantics as shown in #674. This PR passes zero-initialised storage to the constructor instead.

I added a new trait called ZeroInitializable ([dcl.init.general]/6) which initialises all members to 0. In refcount it sets each member to 0, in unsafe it uses MaybeUninit::zeroed().assume_init().

Callers of constructors currently do:

let v = Rc::new(RefCell::new({
  let __v = Rc::new(RefCell::new(<T>::zero_init());
  T::new(__v.as_pointer(), ctor_args);
  Rc::try_unwrap(__v).ok().unwrap().into_inner()
}

This is not the final format, I will fix it in a future PR. I did not add here because the PR is already very big. The final format will be:

let v = Rc::new(RefCell::new(<T>::zero_init());
T::new(v.as_pointer(), ctor_args);

)*};
}

impl_zero_init!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this map needed? You can use Default

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@lucic71

lucic71 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

macOS fails because of master, not because of this PR

Comment thread cpp2rust/converter/converter.cpp Outdated
return std::format("let mut __v: {0} = <{0}>::zero_init();", record_name);
}

std::string Converter::GetPlacementReceiver() const { return "&mut __v"; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should return const char*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DOne

@nunoplopes

Copy link
Copy Markdown
Contributor

Two things I don't like:

  • Naming the constructor new. In Rust, T::new creates a new object. I think we should keep T::new to create a new object, call the constructor via Ptr and return the Rc. The constructor can be called constructor or whatever.
  • There's an Rc created to call the constructor, which is then destroyed and the contents moved to another Rc. This doesn't preserve the this pointer, so it's not valid in C++ as some containers may store it.

@lucic71

lucic71 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author
  • Naming the constructor new. In Rust, T::new creates a new object. I think we should keep T::new to create a new object, call the constructor via Ptr and return the Rc. The constructor can be called constructor or whatever.

Done

  • There's an Rc created to call the constructor, which is then destroyed and the contents moved to another Rc. This doesn't preserve the this pointer, so it's not valid in C++ as some containers may store it.

See the PR description. This is the ideal output:

let v = Rc::new(RefCell::new(<T>::zero_init());
T::construct(v.as_pointer(), ctor_args);

I will do it right after this PR because this one is already very big.

@nunoplopes
nunoplopes merged commit 8f93176 into Cpp2Rust:master Nov 30, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self initialization doesn't work

2 participants